package com.booter.hessian.impl;

import com.booter.hessian.annotation.HessianService;
import com.booter.hessian.service.HessianTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

@Slf4j
@HessianService
@Service
public class HessianTestServiceImpl implements HessianTestService {

    @Override
    public String say(String s) {
        return "xxx==" + s;
    }

    @Override
    public void upload(String filename, InputStream data) {
        try (BufferedInputStream bis = new BufferedInputStream(data);
             BufferedOutputStream bos = new BufferedOutputStream(
                     new FileOutputStream("D:\\s\\" + filename))) {
            byte[] buffer = new byte[8192];
            int r = bis.read(buffer, 0, buffer.length);
            while (r > 0) {
                bos.write(buffer, 0, r);
                r = bis.read(buffer, 0, buffer.length);
            }
        } catch (Exception ex) {
            log.error("", ex);
        }
//
//        BufferedInputStream bis = null;
//        BufferedOutputStream bos = null;
//        try {
//            bis = new BufferedInputStream(data);
//            bos = new BufferedOutputStream(
//                    new FileOutputStream("D:\\s\\" + filename));
//            byte[] buffer = new byte[8192];
//            int r = bis.read(buffer, 0, buffer.length);
//            while (r > 0) {
//                bos.write(buffer, 0, r);
//                r = bis.read(buffer, 0, buffer.length);
//            }
//        } catch (IOException e) {
//            log.error("File upload exception: ", e);
//        } finally {
//            if (bos != null) {
//                try {
//                    bos.close();
//                } catch (IOException e) {
//                    log.error("Closing output exception: ", e);
//                }
//            }
//            if (bis != null) {
//                try {
//                    bis.close();
//                } catch (IOException e) {
//                    log.error("Closing input exception: ", e);
//                }
//            }
//        }
    }
}
